package com.allming.boot.baidu.map.autoconfigure;


import com.allming.boot.baidu.map.service.BaiduMapService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.outfit.boot.okhttp.OkHttpService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;

/**
 * 百度地图自动配置
 * @author ming
 * @date 2020-06-30 11:30
 * <p>company: 杭州行装网络技术有限公司</p>
 * <p>修改履历：</p>
 */
@Configuration
@ConditionalOnClass(BaiduMapService.class)
@ConditionalOnProperty(prefix = "baidu.map", name = "app-key", matchIfMissing = true)
@EnableConfigurationProperties(AppKeyProperties.class)
public class BaiduMapAutoConfiguration {

    private final AppKeyProperties appKeyProperties;
    private final OkHttpService okHttpService;
    public BaiduMapAutoConfiguration(final AppKeyProperties appKeyProperties,
                                     final OkHttpService okHttpService){
        this.appKeyProperties = appKeyProperties;
        this.okHttpService = okHttpService;
    }

    @Bean
    @ConditionalOnMissingBean(BaiduMapService.class)
    public BaiduMapService baiduMapService() {
        return new BaiduMapService(appKeyProperties,okHttpService);
    }
    /**
     * objectMapper
     * @return 自定义 {@link com.fasterxml.jackson.databind.ObjectMapper} 配置
     */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        return objectMapper;
    }
}
